package com.zj.core.adapter

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.fileutils.SPUtils
import com.mcxtzhang.swipemenulib.SwipeMenuLayout
import com.zj.core.R
import com.zj.core.bean.NurseRecResBean
import com.zj.core.interfaces.RecycleAdapterIml
import kotlinx.android.synthetic.main.item_record_recycleview.view.*

/**
 * 日期：2019年3月23日
 * 公司：北京子杰宝贝科技有限公司
 * 地址：北京市朝阳区朝阳农场
 * 开发者：达选文
 *
 * 主体功能：患者操作记录RecycleView适配器
 */
class RecordRecycleAdapter(context: Context?,list: MutableList<NurseRecResBean.DatEntity.RowsEntity>?): RecyclerView.Adapter<RecordRecycleAdapter.RecordViewHolder>(){
    private var mListener: RecycleAdapterIml? = null
    private var mBeanList:MutableList<NurseRecResBean.DatEntity.RowsEntity>?=list
    private var mContext:Context?=context

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecordViewHolder {
        return RecordViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_record_recycleview, parent, false))
    }

    override fun onBindViewHolder(holder: RecordViewHolder, position: Int) {
        with(holder){
            /**
             * 新增
             */
            constrainLayoutItem.setOnClickListener { mListener!!.add(position) }

            /**
             * 历史记录
             */
            addMesBu.setOnClickListener {
                mListener!!.info(mBeanList!![position], position)
                swipMenuLayout.quickClose()
            }

            /**
             * 备注
             */
            referMesBu.setOnClickListener {
                mListener!!.annotation(position)
                swipMenuLayout.quickClose()
            }

            mBeanList!![position].apply {
                mPartentIdTv.text = patientid
                mReferViewTv.text = msg
                mTimeViewTv.text = systime

                /**
                 * 缓存记录
                 */
                val cacheStr = SPUtils.get(mContext,id.toString() + patientid!!, "") as String
                if (cacheStr != "") {
                    mTagTv.visibility = View.VISIBLE
                } else {
                    mTagTv.visibility = View.GONE
                }
            }

            when {
                (position + 1) % 3 == 2 -> {
                    mImageView.setImageResource(R.mipmap.address_3)
                }
                (position + 1) % 3 == 1 -> {
                    mImageView.setImageResource(R.mipmap.address_2)
                }
                else -> {
                    mImageView.setImageResource(R.mipmap.address_1)
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return mBeanList!!.size
    }

    /**
     * 设置Item点击监听
     * @param listener
     */
    fun setItemClickListener(listener: RecycleAdapterIml) {
        this.mListener = listener
    }

    /**
     * 加载更多
     * @param record
     */
    fun onLoadMore(record: List<NurseRecResBean.DatEntity.RowsEntity>) {
        for (mRecordBean in record) {
            mBeanList?.add(mRecordBean)
        }
        notifyDataSetChanged()
    }

    /**
     * 刷新
     */
    fun onRefush(beanList: MutableList<NurseRecResBean.DatEntity.RowsEntity>?) {
        this.mBeanList = beanList
        notifyDataSetChanged()
    }

    inner class RecordViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var swipMenuLayout: SwipeMenuLayout = itemView.swip_menu_layout
        var addMesBu: Button = itemView.add_mes_bu
        var referMesBu: Button = itemView.refer_mes_bu
        var constrainLayoutItem: ConstraintLayout = itemView.constrain_layout_item
        var mTimeViewTv: TextView = itemView.time_view_tv
        var mPartentIdTv: TextView = itemView.partent_id_tv
        var mReferViewTv: TextView = itemView.refer_view_tv
        var mTagTv: TextView = itemView.tag_tv
        var mImageView: ImageView = itemView.address_view_img
    }

    fun clean(){
        if(mListener!=null){
            mListener=null
        }

        if(mBeanList!=null){
            mBeanList?.clear()
            mBeanList=null
        }

        if(mContext!=null){
            mContext=null
        }
    }
}
